
<style>
body{
  background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);

/*  background-image: url('../assets/bg.jpg');
  background-repeat:no-repeat;
  background-size:100% ;*/
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.input {


  border: none;
  padding: 12px 15px;

  width: 240px;
}
.center {
 /* width: 1920px;
  height: 925px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position:absolute;
  width:800px;
  height:400px;
  top:40%;
  left:50%;
  transform:translate(-50%,-50%);*/

  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position: absolute;
  width: 50vw; /* 使用视口宽度的40% */
  height: 20vh; /* 使用视口高度的20% */
  top: 50vh; /* 使用视口高度的50% */
  left: 50vw; /* 使用视口宽度的50% */
  transform: translate(-50%, -50%);
}
h1 {
  font-size: 30px;
  color: #6991c7;
}

.logon {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  /* position: relative;
  overflow: hidden; */
  width: 768px;
  max-width: 100%;
  min-height: 480px;
  margin-top: 20px;
  display: flex;
  background-image: linear-gradient(to top, #a3bded 0%, #6991c7 100%);
  opacity: 0.98; /* 设置元素为半透明 */
}

.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(100%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongright {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}


.overlaytitleH2 {
  font-size: 30px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleP {
  font-size: 15px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitleright {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-100%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaytitle-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.buttongohs {
  width: 180px;
  height: 40px;
  border-radius: 50px;
  border: 1px solid #fff;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
}

.overlaylongH2 {
  font-size: 25px;
  color: #3867B3;
  /* width: 250px; */
}

.overlaylong-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaylong-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}


.inupbutton{
  background-color: #6991c7;
  border: none;
  width: 180px;
  height: 40px;
  border-radius: 50px;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 30px;
}
</style>

<template>

  <div class="center">
    <h1>人力资源管理系统</h1>
    <div class="logon">
      <div :class="overlaylong">
        <div class="overlaylong-Signin" v-if="disfiex == 0">
          <h2 class="overlaylongH2">登录</h2>
          <el-form ref="loginForm" :model="loginFile" :rules="rules" @submit.native.prevent="submitLogin">
          <div class="input">
            <el-form-item prop="uid">
            <el-input
                v-model="loginFile.uid"
                type="text"
                placeholder="id"
                autocomplete="off"
            ></el-input></el-form-item>
          </div>
          <div class="input">
            <el-form-item prop="upassword">
            <el-input
                v-model="loginFile.upassword"
                type="password"
                placeholder="password"
                autocomplete="off"
            ></el-input></el-form-item>
          </div>
          </el-form>
          <el-button class="inupbutton" type="success" round @click="submitLogin('loginFile')">登录</el-button>
<!--          <button class="inupbutton">登录</button>-->
        </div>

        <div class="overlaylong-Signup" v-if="disfiex == 1">
          <h2 class="overlaylongH2">注册账户</h2>
          <el-form ref="registerForm" :model="registerFile" :rules="rules" @submit.native.prevent="submitRegister">
          <div class="input">
            <el-form-item prop="uid">
            <el-input
                v-model="registerFile.uid"
                type="text"
                placeholder="id"
                autocomplete="off"
            ></el-input> </el-form-item>
          </div>
          <div class="input">
            <el-form-item prop="uname">
            <el-input
                v-model="registerFile.uname"
                type="text"
                placeholder="name"
                autocomplete="off"
            ></el-input></el-form-item>
          </div>
          <div class="input">
            <el-form-item prop="upassword">
            <el-input
                v-model="registerFile.upassword"
                type="password"
                placeholder="password"
                autocomplete="off"
            ></el-input> </el-form-item>
          </div>
          </el-form>
          <el-button class="inupbutton" type="success" round @click="submitRegister('registerFile')">注册</el-button>
<!--          <button class="inupbutton">注册</button>-->
        </div>

      </div>
      <div :class="overlaytitle">
        <div class="overlaytitle-Signin" v-if="disfiex == 0">
          <h2 class="overlaytitleH2">没有账号？</h2>
          <p class="overlaytitleP">
            赶紧注册一个吧！！
          </p>
          <div class="buttongohs" @click="Signin">注册</div>
        </div>
        <div class="overlaytitle-Signup" v-if="disfiex == 1">
          <h2 class="overlaytitleH2">已有账号？</h2>
          <p class="overlaytitleP">快快来登录！！</p>
          <div class="buttongohs" @click="Signup">登录</div>
        </div>
      </div>
    </div>

  </div>


</template>

<script>
import store from '@/store';
export default {

  data() {
    var checkId = (rule, value, cb) => {
      const regId = /^\d+$/;

      if (regId.test(value)) {
        return cb();
      }
      cb(new Error('请输入合法的账号（数字组成）'));
    };
    return {
      overlaylong: 'overlaylong',
      overlaytitle: 'overlaytitle',
      disfiex: 0,
      registerFile:{
        uid: '',
        uname: '',
        upassword: '',
      },
      loginFile:{
        uid:'',
        uname: '',
        upassword:'',
        upost:'',
      },
      //添加表单的验证规则对象
      rules: {
        uid: [
          { required: true, message: '请输入ID', trigger: 'blur' },
          { validator: checkId, trigger: 'blur' }
        ],
        uname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 15, message: '用户名长度在 1到 15 个字符', trigger: 'blur' }
        ],
        upassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 15, message: '密码长度在 1到 15 个字符', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    Signin() {
      this.overlaylong = "overlaylongleft"
      this.overlaytitle = "overlaytitleright"
      setTimeout(() => {
        this.disfiex = 1
      }, 200)
    },
    Signup() {
      this.overlaylong = "overlaylongright"
      this.overlaytitle = "overlaytitleleft"

      setTimeout(() => {
        this.disfiex = 0
      }, 200)

    },
    submitRegister(formName) {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.axios.post('/user/register', this.registerFile, {
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => {
            if(res.data){
              alert("注册成功！快去登录");
              this.overlaylong = "overlaylongright"
              this.overlaytitle = "overlaytitleleft"
            }
            else {
              alert(res.data.message);
            }

            setTimeout(() => {
              this.disfiex = 0
            }, 200)

          }).catch(error => {
            console.error("注册失败：", error);
          });

        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    submitLogin(formName) {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.axios.post('/user/login', this.loginFile, {
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => {
            if (res.data) {
              console.log("登录成功");
              alert("登录成功");
              this.loginFile = res.data.data;
              console.log(this.loginFile);
              this.$store.dispatch('loginUser', this.loginFile);
              this.$router.push("/index");
            } else {
              alert(res.data.message);
            }
          }).catch(error => {
            console.error("登录失败：", error);
          });
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    }
  }
}
</script>